
//===============合同清单==============================

var datas=[];
//var productIdList=[];
var tableIdQingDan="bootstrap-table-qingdan";
var options = {
    data:datas,
    id:tableIdQingDan,
    //url:  modelType=="edit"?ctx + "contract/contractProduct/list?contractId="+contractId:'',
    removeUrl: ctx + "contract/contractProduct/remove",
    createUrl: ctx + "contract/contractProduct/add?contractType="+contractType,
    onEditableSave: onEditableSave,
    showSearch: false,
    showRefresh: false,
    showColumns: false,
    showToggle: false,
    uniqueId: "productId",
    modalName: "合同清单",
    columns: [{
        field: 'checkbox',
        checkbox: true
    },
        {
            field: 'contractProductId',
            title: 'ID',
            visible: false
        },
        {
            field: 'productId',
            title: '产品ID',
            visible: false
        },
        {
            field: 'name',
            title: '产品名称',
        },
        {
            field: 'unit',
            title: '产品单位',
        },
        {
            field: 'img',
            title: '产品图片2',
            visible: false,
            formatter: function(value, row, index) {
                return $.table.imageView(value);
            }
        },
        {
            field: 'price',
            title: '价格',
            editable : {
                type : 'text',
                title : '产品价格',
                validate : function(value) {
                    if (value.length == 0) {
                        return '产品价格不能为空';
                    }
                }
            }
        },
        {
            field: 'num',
            title: '清单总数量',
            editable : {
                type : 'text',
                title : '总数量',
                validate : function(value) {
                    if (value.length == 0) {
                        return '总数量不能为空';
                    }
                }
            }
        }
    ]
};
$(function() {
    if(modelType=="edit"){//编辑
        $.ajax({
            type: "POST",
            url: ctx + "contract/contractProduct/list?contractId="+contractId,
            success: function(data) {
                if(data.code==0){
                    datas = data.rows;
                    options.data = datas;
                    $.table.init(options);
                }else{
                    $.modal.alertWarning(data.msg);
                }

            }
        });
    }else{
        $.table.init(options);
    }


});

//选择产品
function chooseProduct(){
    table.set(tableIdQingDan);
    var title="选择产品";
    if(contractTypeObj.buy==contractType){
        title="选择采购产品";
    }
    if(contractTypeObj.sell==contractType){
        title="选择销售产品";
    }
    $.modal.open(title,ctx + "product/product?type=chooseProduct",1200,($(window).height() - 50),function (index, layero) {
        var rows = layero.find('iframe')[0].contentWindow.getMallProductSelections();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        } else {
            handRowsValue(rows);
            $.modal.close(index);
        }
    });
}


/*
* 添加产品
* */
function handRowsValue(rows){
  // var arry= mergeAndDistinct(datas.rows);
   for(var i=0;i<rows.length;i++){
       var row={};
       row.checkbox=false;
       row.cartId='';
       row.productId=rows[i].productId;
       row.name=rows[i].name;
       row.unit=rows[i].unit;
       row.img=rows[i].img;
       row.price=rows[i].price;
       row.num=rows[i].remainNum||0;
      // productIdList.push(row)
       if(isContainProduct(row)){//包含不做处理
           continue;
       }else{
          // 不包含添加产品;
           datas.push(row)
       }

   }
    options.data=datas;
    $.table.destroy(tableIdQingDan);
    $.table.init(options);
}

function isContainProduct(row){
   let flag =false;
    for(let i=0;i<datas.length;i++){
        if(datas[i].productId==row.productId){//原来有的就用旧的数据
            flag=true;break;
        }
    }
    return flag;
}

function addQingDan(){
    table.set("bootstrap-table-qingdan");
    var url=ctx + "contract/contractProduct/add?contractType=1";
    $.modal.open("添加清单",url,1200,($(window).height() - 50),function (index, layero) {
        var iframeWin = layero.find('iframe')[0];
        iframeWin.contentWindow.submitHandler(index, layero);
        //$.table.refresh("bootstrap-table-file",null,null,ctx + "contract/contractProduct/list");
        $.table.refresh();
    });
}
//删除
function removeQingDan(){
    var ids = $.table.selectColumns("productId");
    if (ids.length == 0) {
        $.modal.alertWarning("请至少选择一条记录");
        return;
    }
    $("#"+tableIdQingDan).bootstrapTable('remove', {
        field: 'productId',
        values: ids
    })
}


function getAllQingDanData(){
    var data = $("#" +tableIdQingDan).bootstrapTable('getData');
    //var data = $("#" +tableIdQingDan).bootstrapTable('getSelections');
    if(data.length==0){
        $.modal.alertWarning("清单请至少有一样产品");
        return;
    }else if(data.length>0){
        for(var i=0;i<data.length;i++){
            if(data[i].price==""||data[i].price==null ||data[i].price==0){
                data[i].checkbox=false;
                $.modal.alertWarning(data[i].name+"：请填写产品价格");
                return;
            }
            if(data[i].num==""||data[i].num==null || data[i].num==0){
                data[i].checkbox=false;
                $.modal.alertWarning(data[i].name+"：请填写产品数量");
                return;
            }
        }
    }
    // alert(JSON.stringify(data))
    return JSON.stringify(data);
}
function onEditableSave (field, row, rowIndex, oldValue, $el) {
    datas[rowIndex].num=row[field];
    //alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
}
//===============end合同附件==============================
